how to manage array in 8086 programming ?

Tutorial Details


Completion Time: 30 min
Difficulty: Intermediate
Technology: 8086
Language: Assembly


This entry is part 1 of Learn to program in 8086

8086Series how to manage array in 8086 programming ?

[hr]

This tutorial teaches you ho to manage array in 8086 programming. It primary focus on declaration and access mode and finally some example with source codes.


1. Declare an array

To declare an array, you will use specify the name of your array, the dimension of your array, the size of every element and the special system words DUP.
Size of element can be DB( for byte) or DW ( for word that means 2 bytes).
Example

A   DB  1, 2, 3, 4, 5, 6, 7, 8, 10
B   DB  DIM  DUP(?)     

A is an array with inital values. B is an array of dimension DIM (a constant) without initial values.


2. Access to array elements

This part is easy similar to other language, you need the right index and then you access your element.
That’s means if i want to access the first element of the array A, I just write: A[0].


3. Sum array elements

Let’s assume we have an Array A, we will compute the sum for each pair of consecutive values of A, and put the result in a secon array b. We will use this formula: elements array (Bj = Aj + Aj+1).

The principle is simple, we will do a cycle and your all the element but not the last, we will do the sum and assing it to the new array.

; Sum array elements
DIM1   EQU 9  
       .MODEL small
       .STACK
       .DATA
A   DB  1, 2, 3, 4, 5, 6, 7, 8, 10, 11
B   DB  DIM1  DUP(?)   
       .CODE
       .STARTUP 
;1.  Compute the sum for each pair of consecutive values, 
;putting the result in a 9 elements array
; (Bj = Aj + Aj+1).
       MOV AL, 0
       MOV CX, DIM1
       MOV DI, 0
lab1:  MOV AL,A[DI]
       ADD AL,A[DI+1]
       MOV B[DI],AL
       INC DI
       DEC CX
       CMP CX,0
       JNZ lab1  
       .EXIT
       END

4. Find the min value of an array

Let’s assume we have an Array A, we will find the min value of this array. We use this algorithm:
small = element[0]
for each element in array, starting from 1 (not 0):
if (element < small) small = element

; Sum array elements
DIM1   EQU 10  
       .MODEL small
       .STACK
       .DATA
A   DB  1, 2, 3, 4, 5, 6, 7, 8, 10, 11 
MINA DB ?
       .CODE
       .STARTUP 
     MOV DI, 0
     MOV AL,A[DI]
     MOV CX, DIM1
     MOV DI, 1  
LAB1:CMP A[DI],AL
     JAE PASS
     MOV AL,A[DI]
PASS:INC DI
     DEC CX
     CMP CX,0             
     JNZ LAB1
     MOV MINA,AL
    .EXIT
   END    

5. Create a 2 dimensional array

Let’s assume we have 2 arrays A and B of dimension DIM. We will compute all possible products among values
of first array and values of second array, we will put results in a matrix of DIMxDIM dimension
Here is the algorithm I will use:

for each element1 in array A, starting from 0:
for each element2 in array B, starting from 0:
M[i][j] = element1*element2

Here we use two trick.
In 8086, there is no real structure for 2 dimensional array in memory.
So we will map our matrix in a linear memory. That’s mean we declare an array od DiM*DIM elements and every element of the matrix will be in that array.
The mapping is simple to access to the Jth row and the Kth colum we just do: M[J,K] = M[J * DIM + K]

The second trick is about the size of every element of the matrix. Since every element is a product of two element of 1 byte , we will use 2 bytes for the size of every element of the matrix.
So the mapping formula is now: M[J,K] = M[2 * (J * DIM + K)] because every element take one more byte on memory.

The result:

DIM   EQU 9  
       .MODEL small
       .STACK
       .DATA
A   DB  1, 2, 3, 4, 5, 6, 7, 8, 10
B   DB  11, 12, 13,14, 15, 16, 17, 18, 19    
M   DW  (DIM * DIM) DUP (?)
C DB ?
TEMP DW ?   
TEMP2 DW ?
.CODE
       .STARTUP 
       MOV AX, 0
       MOV CX, DIM1
       MOV DI, 0
lab5:  MOV C, DIM1
       MOV BX, 0
lab6:  MOV AX, 0
       MOV AL, A[DI]
       MUL B[BX]         
;M[J,K] = M[2 * (J * NUM_COLS + K)]
       MOV TEMP,AX
       XOR AX,AX
       MOV AX,DI
       MOV TEMP2,BX
       MOV BX,DIM1
       MUL BX
       MOV BX,TEMP2
       ADD AX,BX
       MOV TEMP2,BX
       MOV BX,2
       MUL BX
       MOV BX,TEMP2  
       MOV TEMP2,DI
       MOV SI,AX
       MOV AX,TEMP
       MOV M[SI],AX 
       MOV AX, 0
       INC BX
       DEC C
       CMP C,0             
       JNZ LAB6
       MOV AX, 0
       INC DI
       DEC CX
       CMP CX,0             
       JNZ LAB5 
        .EXIT
       END

6. Find the max value of 2 dimensional array

Let assume we have a matrix, we will find the maximum value among values of the matrix.
Here is the algorithm we use:
max = matrix[0][0];
for each integer i from 0 to dim
for each integer j from 0 to dim
if (max < matrix[i][j]) { max = matrix[i][j];

DIM   EQU 3  
       .MODEL small
       .STACK
       .DATA
M   DB  1, 2, 3, 4, 5, 6, 7, 8, 10
C DB ?
MAX DW ?
TEMP DW ?   
TEMP2 DW ?
       .CODE
       .STARTUP 
MOV AX,M[0]
      MOV CX,DIM  
      MOV DI,0
lab7: MOV C,DIM
      MOV BX, 0 
lab8: ;M[J,K] = M[2 * (J * NUM_COLS + K)]
       MOV TEMP,AX
       XOR AX,AX
       MOV AX,DI
       MOV TEMP2,BX
       MOV BX,DIM
       MUL BX
       MOV BX,TEMP2
       ADD AX,BX
       MOV TEMP2,BX
       MOV BX,2
       MUL BX
       MOV BX,TEMP2  
       MOV TEMP2,DI
       MOV SI,AX
       MOV AX,TEMP
      CMP AX,M[SI]
      JAE PASS3
      MOV AX,M[SI]
PASS3:INC BX
      DEC C
      CMP C,0             
      JNZ LAB8 
      INC DI
      DEC CX
      CMP CX,0             
      JNZ LAB7
      MOV MAX,AX    
   .EXIT
   END 

7. Source code


8. Links and Literature

art of assembly

 how to manage array in 8086 programming ? how to manage array in 8086 programming ?


9. Thank you

Support my challenge, support my blog. Please Share this article if you found it useful. Thank you.

Precedente The Beginner’s Guide to 8086 Assembly programming Successivo How to handle characters in 8086 programming ?

3 commenti su “how to manage array in 8086 programming ?

Lascia un commento

This site uses Akismet to reduce spam. Learn how your comment data is processed.